<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - OAuth2授权</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-container {
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 30px;
            width: 350px;
        }
        h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #333;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #666;
        }
        input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #4285f4;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 14px;
            cursor: pointer;
            margin-top: 10px;
        }
        button:hover {
            background-color: #357ae8;
        }
        .error {
            color: #f44336;
            font-size: 14px;
            margin-top: 10px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>用户登录</h2>
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required>
            </div>
            
            <!-- 隐藏的OAuth2授权请求参数 -->
            <input type="hidden" id="client_id" name="client_id" th:value="${client_id}">
            <input type="hidden" id="response_type" name="response_type" th:value="${response_type}">
            <input type="hidden" id="redirect_uri" name="redirect_uri" th:value="${redirect_uri}">
            <input type="hidden" id="scope" name="scope" th:value="${scope}">
            <input type="hidden" id="state" name="state" th:value="${state}">
            
            <div class="error" id="errorMsg"></div>
            <button type="submit">登录</button>
        </form>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            console.log('登录页面加载完成');
            // 输出当前页面OAuth2参数
            console.log('参数信息: ', {
                client_id: document.getElementById('client_id').value,
                response_type: document.getElementById('response_type').value,
                redirect_uri: document.getElementById('redirect_uri').value,
                scope: document.getElementById('scope').value,
                state: document.getElementById('state').value
            });
        });
    
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const errorMsg = document.getElementById('errorMsg');
            
            if (!username || !password) {
                errorMsg.textContent = '用户名和密码不能为空';
                return;
            }
            
            console.log('尝试登录，用户名：' + username);
            
            // 发送登录请求
            fetch('/oauth2/doLogin', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: username,
                    password: password
                }),
                credentials: 'include' // 确保包含cookies
            })
            .then(response => {
                console.log('收到登录响应，状态码：', response.status);
                return response.json();
            })
            .then(data => {
                console.log('登录响应数据：', data);
                
                if (data.code === 200) {
                    console.log('登录成功，服务器返回：', data);
                    
                    // 保存token到localStorage，如果Sa-Token配置为从localStorage读取token
                    if (data.data && data.data.token) {
                        localStorage.setItem('Authorization', data.data.token);
                        console.log('已保存token到localStorage');
                    }
                    
                    // 登录成功后，重新拼接授权请求URL
                    const client_id = document.getElementById('client_id').value || '';
                    const response_type = document.getElementById('response_type').value || 'code';
                    const redirect_uri = document.getElementById('redirect_uri').value || '';
                    const scope = document.getElementById('scope').value || '';
                    const state = document.getElementById('state').value || '';
                    
                    console.log('登录成功，准备重定向，参数如下：');
                    console.log('client_id:', client_id);
                    console.log('response_type:', response_type);
                    console.log('redirect_uri:', redirect_uri);
                    console.log('scope:', scope);
                    console.log('state:', state);
                    
                    if (!client_id || !redirect_uri) {
                        console.error('缺少必要参数，无法跳转到授权页面');
                        errorMsg.textContent = '授权参数不完整，请返回重试';
                        return;
                    }
                    
                    // 构建授权URL并跳转
                    let authUrl = '/oauth2/authorize?client_id=' + encodeURIComponent(client_id);
                    if (response_type) authUrl += '&response_type=' + encodeURIComponent(response_type);
                    if (redirect_uri) authUrl += '&redirect_uri=' + encodeURIComponent(redirect_uri);
                    if (scope) authUrl += '&scope=' + encodeURIComponent(scope);
                    if (state) authUrl += '&state=' + encodeURIComponent(state);
                    
                    console.log('授权页面URL:', authUrl);
                    
                    // 模拟延迟跳转，确保cookies等状态已保存
                    setTimeout(function() {
                        // 跳转到授权页面
                        window.location.href = authUrl;
                    }, 500);
                } else {
                    // 显示错误信息
                    console.error('登录失败:', data);
                    errorMsg.textContent = data.message || '登录失败，请检查用户名和密码';
                }
            })
            .catch(error => {
                console.error('登录请求发生错误:', error);
                errorMsg.textContent = '网络错误，请稍后再试';
            });
        });
    </script>
</body>
</html> 